import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/home', // 重定向:重新指向其它path,会改变网址
    },
    {
        path: '/home',
        name: 'Home',
        component: () =>
            import ('../views/Home.vue'),
        children: [{
                path: '/home',
                redirect: '/home/shop', // 重定向:重新指向其它path,会改变网址
            },
            {
                path: '/home/user',
                name: 'UserList',
                component: () =>
                    import ('../views/home/UserList.vue'),
            }, {
                path: '/home/shop',
                name: 'ShopList',
                component: () =>
                    import ('../views/home/ShopList.vue'),
            }
        ]
    },
    {
        path: '/about',
        name: 'About',
        component: () =>
            import ('../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue')
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

let whiteList = ['/', '/login'] //白名单
router.beforeEach((to, from, next) => {
    let isToken = localStorage.getItem('token')
        // console.log(isToken);
    let isTO = whiteList.includes(to.path)
    if (isTO) {
        next()
    } else if (isToken) {
        next()
    } else {
        next('/login')
    }
})
export default router